package com.example.demoproject;

import android.os.Bundle;

import com.example.demoproject.fragment.DynamicFragment1;
import com.example.demoproject.fragment.DynamicFragment2;
import com.example.demoproject.fragment.IFragmentCallBack;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.util.Log;
import android.view.View;
import android.widget.Button;

public class FragmentActivity extends AppCompatActivity{
    boolean flag = true;
    String message = "你好啊，这是来自FragmentActivity的信息！";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);
        Button button = findViewById(R.id.fragmentBtn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("lfy","[FragmentActivity] onClick...flag="+flag);
                FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                Bundle bundle = new Bundle();
                bundle.putString("msg","我是activity上的信息！！！");
                DynamicFragment1 dy1 = new DynamicFragment1();
                dy1.setArguments(bundle);

                DynamicFragment2 dy2 = new DynamicFragment2();
                transaction.replace(R.id.dynamicFragment,(flag ? dy1 : dy2));
                transaction.commit();
                flag = !flag;

                dy2.setCallBack(new IFragmentCallBack() {
                    @Override
                    public void setMsgToActivity(String msg) {
                        Log.d("lfy","[FragmentActivity] setMsgToActivity...msg="+msg);
                    }

                    @Override
                    public String getMsgFromActivity() {
                        return message;
                    }
                });


            }
        });
    }

};
